package cn.wolfcode.service.impl;

import cn.wolfcode.advice.DeptNameUniqueException;
import cn.wolfcode.domain.Dept;
import cn.wolfcode.domain.Result;
import cn.wolfcode.mapper.DeptMapper;
import cn.wolfcode.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void delete(Long id) {
        deptMapper.delete(id);
    }

    @Override
    public Result save(Dept dept) {
       Dept dept1= deptMapper.selByDeptName(dept.getName());
       if(dept1!=null){
           throw new DeptNameUniqueException();
       }
        deptMapper.save(dept);
       return Result.success();
    }

    @Override
    public Dept getById(Long id) {
       return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }
}
